package com.kyuketsukistudios.mikatsuki.assetmanager 
{
	import com.carlcalderon.arthropod.Debug;
	import com.kyuketsukistudios.mikatsuki.data.SingletonData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author RZEROSTERN
	 * A beautiful Asset Manager for inserting all of the assets we'll use on the game.
	 * Comparing with SingletonData class, this one will only handle assets, no matter if
	 * they are on different SWF files.
	 */
	public class AssetManager
	{	
		/*Variables*/
		private var m_assetLoader:Loader;
		private var loadingCompletedListener:Function = null;
		private var m_loaderInfo:LoaderInfo;
		
		/*The constructor*/
		public function AssetManager(p_listener:Function, p_lib_name:String) 
		{
			loadLibrary(p_listener, p_lib_name); //We'll load our library, unfortunately I couldn't make it Singleton T_T
		}
		
		/* Public functions.
		 * This function is the main loader of our libraries. We can call any SWF file.*/
		public function loadLibrary(p_listener:Function, p_lib_name:String):void
		{
			var path:String; //The string path
			var url:URLRequest; //This var converts the path to a URL.
			m_assetLoader = new Loader(); //We call a new instance of the Loader class.
			
			try
			{
				this.loadingCompletedListener = p_listener;
				m_assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
				path = "./lib/" + p_lib_name + "Lib.swf";
				url = new URLRequest(path); //We make our URL for loading the library.
				m_assetLoader.load(url); //GO, GO, GO !!! LOAD THIS SHIT.
				m_assetLoader.dispatchEvent(new Event("STARTED"));
			}
			catch (error:Error)
			{
				/*One of two: You didn't create the lib or your path is wrong.*/
				throw Error("AssetManager Error.- The library was not found.");
			}
			
		}
		
		/* Universal function for calling our assets. It depends from the 
		 * assetLibrary so we need to load our library first for load our 
		 * assets from that library.
		 */
		public function getMyAsset(p_asset_name:String, p_kind:String):*
		{
			var tempClass:Class = this.getAsset(p_asset_name);
			
			if (tempClass)
			{
				var asset:*;
				switch(p_kind)
				{
					case "mc":
						asset = new tempClass() as MovieClip;
					break;
					
					case "spr":
						asset = new tempClass() as Sprite;
					break;
				}
				SingletonData.getInstance().sendToConsole(0x00ff00, "Asset " + 
													String(asset) + " has been loaded.");
				return asset;
			}
		}
		/* This function will get the asset we need.
		 * But we will return it as a CLASS.*/
		public function getAsset(p_asset:String):Class
		{
			if (m_assetLoader.contentLoaderInfo.applicationDomain.hasDefinition(p_asset))
			{
				return m_assetLoader.contentLoaderInfo.applicationDomain.getDefinition(p_asset) as Class;
			}
			return null;
		}
		
		/* Private functions
		 * When we finish... this is it !!!*/
		private function onLoadingComplete(p_event:Event):void 
		{
			loadingCompletedListener();
		}
	}

}